import { Color, color } from "cc";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { TableRoleModel } from "../../common/table/TableRoleModel";
import { RoleAttributeType, RoleBattleType, RoleModuleType } from "./RoleEnum";
import { RoleNumericMap } from "./attribute/RoleNumericMap";
import { smc } from "../../common/ecs/SingletonModuleComp";
import { BattleType } from "../../battle/scene/SceneEvent";

/** 
 * 角色属性数据
 * 1、角色数值属性
 * 2、角色占网格面积
 */
@ecs.register('RoleModel')
export class RoleModelComp extends ecs.Comp {
    /** 提供MVVM组件使用的数据 */
    vm: any = {};
    /** 武将 type:1 主角模型，2 武将模型，3 小怪  */
    table: TableRoleModel = null!;
    /** 武将文字颜色 */
    private nameColorList: Color[] = [color(242, 169, 255, 255), color(240, 160, 74, 255), color(255, 56, 52, 255)]
    public nameColor: Color = null!;
    /** 角色数值属性 */
    attributes: RoleNumericMap = null!;
    roleType: RoleBattleType = null!;
    /** 是否是自己阵容 */
    myTeam: boolean = true;
     /** 本回合是否已行动 */
     active: boolean = false;
    constructor() {
        super();
        this.attributes = new RoleNumericMap(this.vm);
    }
    /** 角色服务器唯一编号 */
    uuid: number | string = 0;
    /** 编号 */
    get id(): number {
        return this.table.id;
    }
    set id(value: number) {
        if (this.table == null) this.table = new TableRoleModel();
        this.table.init(value);
        this.nameColor = this.nameColorList[this.table.color - 1];
        if(value==2){
            console
        }
        for (const key in RoleAttributeType) {
            if ((this.table as any)[key]) {
                this.attributes.get(key as RoleAttributeType).setValue(RoleModuleType.Base, (this.table as any)[key]);
            }
            else {
                this.attributes.get(key as RoleAttributeType).setValue(RoleModuleType.Base, 0);
            }
        }
        this.attributes.get(RoleAttributeType.power_max).setValue(RoleModuleType.Base, 100);
        this.vm["id"] = this.table.id;
        this.vm["cardName"] = this.table.name;
        this.vm["iconHeros"] = `res/icons/heroes/${this.table.nickName}`;
        this.star = 0;
        this.stage = 0;
        this.level = 1;
        this.levelMax = 100;
        this.pos = 0;
    }
    get spinePath(): string {
        var nickName = this.table.nickName;
        if (this.table.type == 2) {
            return `content/role/${nickName}/${nickName}`;
        } else {
            return `content/enemy/${nickName}/${nickName}`;
        }
    }
    /** 战斗类型 */
    public battleType!: BattleType;
    /**
        * 是否开启
        */
    public hasOpen: number = 0;

    /**进阶：+1 */
    private _stage: number = 0;
    get stage(): number {
        return this._stage;
    }
    set stage(value: number) {
        this._stage = value;
        this.vm["stage"] = this._stage;
    }

    /**星 */
    private _star: number = 0;
    get star(): number {
        return this._star;
    }
    set star(value: number) {
        this._star = value;
        this.vm["star"] = this._star;

        if (this._star == 0) {
            this.vm["starNum"] = 0;
            this.vm["starLvPath"] = `gui/role_page/texture/star/xing1`;
        } else {
            this.vm["starNum"] = this._star % 5 == 0 ? 5 : this._star % 5;
            this.vm["starLvPath"] = `gui/role_page/texture/star/xing${Math.ceil(this._star / 5)}`;
        }
    }

    /**等级 */
    private _level: number = 1;
    get level(): number {
        return this._level;
    }
    set level(value: number) {
        this._level = value;
        this.vm["level"] = this._level;
    }

    /** 当前阶最大等级 */
    private _levelMax: number = 100;
    get levelMax(): number {
        return this._levelMax;
    }
    set levelMax(value: number) {
        this._levelMax = value;
        this.vm["levelMax"] = this._levelMax;
    }

    /** 上阵武将位置 */
    private _pos: number = 0;
    get pos(): number {
        return this._pos;
    }
    set pos(value: number) {
        this._pos = value;
        this.vm["pos"] = this.pos;
    }
    /** 碎片id */
    public get clipId(): string {
        return `card_${this.table.id}`;
    }
    reset(): void {
        this.uuid = -1;
        this.star = 0;
        this.stage = 0;
        this.level = 1;
        this.levelMax = 100;
        this.pos = 0;
        this.myTeam = true;
        this.active = false;
    }

}